import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UserService } from '../user/user.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
  ) {}

  async signIn(username: string, password: string) {
    const user = await this.userService.findOne(username, password);
    if (!user) {
      throw new UnauthorizedException('Invalid username or password');
    }
    const { password: p, ...userinfo } = user.toObject(); //eslint-disable-line
    // return userinfo;
    return {
      token: await this.jwtService.signAsync(userinfo),
    };
    // return {
    //   username: user.username,
    //   nickname: user.nickname,
    // };
  }
}
